package com.vise.bledemo.jky;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.hjq.toast.ToastUtils;
import com.vise.baseble.common.PropertyType;
import com.vise.bledemo.R;
import com.vise.bledemo.common.BluetoothDeviceManager;
import com.vise.bledemo.common.ToastUtil;
import com.vise.bledemo.event.ConnectEvent;
import com.vise.bledemo.utils.ConvertUtil;
import com.vise.xsnow.event.BusManager;
import com.vise.xsnow.event.Subscribe;

import java.util.UUID;

public class BLEDeviceControlActivity extends BaseBLEActivity {

    public static void startBLEDeviceControlActivity(Activity activity) {
        activity.startActivity(new Intent(activity, BLEDeviceControlActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bledevice_control);
        BusManager.getBus().register(this);
        initBT();
        initK1Click();
        initK2Click();
        initK3Click();
        initK4Click();
        initALLClick();
    }

    private void initALLClick() {
        findViewById(R.id.all_open).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K1\":\"BT_ON\",\"K2\":\"BT_ON\",\"K3\":\"BT_ON\",\"K4\":\"BT_ON\"}";
                BluetoothDeviceManager.getInstance().write1(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.all_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K1\":\"BT_OFF\",\"K2\":\"BT_OFF\",\"K3\":\"BT_OFF\",\"K4\":\"BT_OFF\"}";
                BluetoothDeviceManager.getInstance().write1(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

    }

    private void initK4Click() {
        findViewById(R.id.id_4_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K4\":\"BT_ON\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_4_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K4\":\"BT_OFF\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_4_3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K4\":\"SW_ON\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_4_4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K4\":\"SW_OFF\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_4_5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K4\":\"BT_ON\",\"TIMEOUT_K4\":10}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });
    }

    private void initK3Click() {
        findViewById(R.id.id_3_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K3\":\"BT_ON\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_3_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K3\":\"BT_OFF\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_3_3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K3\":\"SW_ON\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_3_4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K3\":\"SW_OFF\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_2_5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K3\":\"BT_ON\",\"TIMEOUT_K3\":10}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });
    }

    private void initK2Click() {
        findViewById(R.id.id_2_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K2\":\"BT_ON\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_2_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K2\":\"BT_OFF\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_2_3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K2\":\"SW_ON\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_2_4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K2\":\"SW_OFF\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_2_5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K2\":\"BT_ON\",\"TIMEOUT_K2\":10}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });
    }

    private void initK1Click() {
        findViewById(R.id.id_1_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K1\":\"BT_ON\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_1_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K1\":\"BT_OFF\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_1_3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K1\":\"SW_ON\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_1_4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K1\":\"SW_OFF\"}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });

        findViewById(R.id.id_1_5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = "{\"K1\":\"BT_ON\",\"TIMEOUT_K1\":10}";
                BluetoothDeviceManager.getInstance().write(mDevice, ConvertUtil.hexStr2Bytes(ConvertUtil.str2HexStr(temp)));
                reConnected();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BluetoothDeviceManager.getInstance().disconnect(mDevice);
        BusManager.getBus().unregister(this);
    }

    private void initBT() {
        if (mDevice != null && BluetoothDeviceManager.getInstance().isConnected(mDevice)) {
            ToastUtil.showToast(this, "蓝牙链接成功 执行写入服务！！！");
            BluetoothDeviceManager.getInstance().bindChannel(mDevice, PropertyType.PROPERTY_WRITE, UUID.fromString("19b10000-e8f2-537e-4f6c-d104768a1214"),
                    UUID.fromString("19b10001-e8f2-537e-4f6c-d104768a1214"), null);
//            BluetoothDeviceManager.getInstance().bindChannel(mDevice, PropertyType.PROPERTY_NOTIFY, UUID.fromString("19b10001-e8f2-537e-4f6c-d104768a1214"), UUID.fromString("19b10001-e8f2-537e-4f6c-d104768a1214"), null);
//            BluetoothDeviceManager.getInstance().write(BTListRecycleViewAdapter.selectBTDevices, HexUtil.decodeHex("22110000".toString().toCharArray()));
        } else {
            ToastUtil.showToast(this, "没有选择蓝牙设备！！！");
        }
    }

    ProgressDialog progressDialog;

    private void reConnected() {
        progressDialog = ProgressDialog.show(BLEDeviceControlActivity.this, "蓝牙操作提示", "控制页面 蓝牙正在重连  请稍等 连接中 ..."
                , false, true);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                BluetoothDeviceManager.getInstance().connect(mDevice);
            }
        }, 1000);
    }


    @Subscribe
    public void showConnectedDevice(ConnectEvent event) {
        if (event != null) {
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
            if (event.isSuccess()) {
                ToastUtil.showToast(BLEDeviceControlActivity.this, "控制页面 蓝牙连接成功!");
                initBT();
            } else {
                if (event.isDisconnected()) {
                    ToastUtil.showToast(BLEDeviceControlActivity.this, "控制页面 蓝牙已经断开断开");

                } else {
                    ToastUtil.showToast(BLEDeviceControlActivity.this, "控制页面 蓝牙连接失败!");
                }
            }
        }
    }

}